﻿using System;
using System.Globalization;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.CommonSenseControls;
using SenseTimeTracker.Main;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;

namespace SenseTimeTracker.Option
{
	public class OptionSliderPanel : SliderPanel
	{
		private SensePanelDateTimeItemEx defaultStartTimeItem;
		private SensePanelDateTimeItemEx defaultFinalTimeItem;
		private SensePanelDateTimeItemEx defaultDailyWorkingTimeItem;
		private SensePanelComboItem languageComboItem;

		public OptionSliderPanel(SenseTimeTrackerForm form)
			: base(form, "OptionSmallIcon.png", "OptionLargeIcon.png")
		{
			defaultStartTimeItem = new SensePanelDateTimeItemEx();
			defaultStartTimeItem.DisplayStyle = SensePanelDateTimeItem.Style.Time;
			defaultStartTimeItem.OnDateTimeChanged += defaultStartTimeItem_OnDateTimeChanged;
			defaultStartTimeItem.ShowSeparator = true;

			defaultFinalTimeItem = new SensePanelDateTimeItemEx();
			defaultFinalTimeItem.DisplayStyle = SensePanelDateTimeItem.Style.Time;
			defaultFinalTimeItem.OnDateTimeChanged += defaultFinalTimeItem_OnDateTimeChanged;
			defaultFinalTimeItem.ShowSeparator = true;

			defaultDailyWorkingTimeItem = new SensePanelDateTimeItemEx();
			defaultDailyWorkingTimeItem.DisplayStyle = SensePanelDateTimeItem.Style.Time;
			defaultDailyWorkingTimeItem.OnDateTimeChanged += defaultDailyWorkingTimeItem_OnDateTimeChanged;
			defaultDailyWorkingTimeItem.ShowSeparator = true;

			languageComboItem = new SensePanelComboItem();
			languageComboItem.OnSelectedValueChanged += languageComboItem_OnSelectedValueChanged;
			languageComboItem.ShowSeparator = true;

		}

		void languageComboItem_OnSelectedValueChanged(object Sender, object Value)
		{
			switch ((AvailableLanguage)Value)
			{
				case AvailableLanguage.en:
					Persistance.Instance.SelectedLanguage = CultureInfo.CreateSpecificCulture("en");
					break;
				case AvailableLanguage.de:
					Persistance.Instance.SelectedLanguage = CultureInfo.CreateSpecificCulture("de");
					break;
				case AvailableLanguage.nl:
					Persistance.Instance.SelectedLanguage = CultureInfo.CreateSpecificCulture("nl");
					break;
				default:
					Persistance.Instance.SelectedLanguage = CultureInfo.InvariantCulture;
					break;
			}

			ownedForm.UpdateHeader();
			Bind(senseListControl);
			ownedForm.menuItemTimeTracker.Text = Resources.MenuItemTimeTracker;
			ownedForm.menuItemMenu.Text = Resources.MenuItemMenu;
		}


		void defaultStartTimeItem_OnDateTimeChanged(object Sender, DateTime DateTime)
		{
			if (DateTime.TimeOfDay > defaultFinalTimeItem.DateTime.TimeOfDay)
			{
				defaultStartTimeItem.DateTime = defaultFinalTimeItem.DateTime;
			}

			Persistance.Instance.DefaultStartTime = defaultStartTimeItem.DateTime;
		}

		void defaultFinalTimeItem_OnDateTimeChanged(object Sender, DateTime DateTime)
		{
			if (DateTime.TimeOfDay < defaultStartTimeItem.DateTime.TimeOfDay)
			{
				defaultFinalTimeItem.DateTime = defaultStartTimeItem.DateTime;
			}

			Persistance.Instance.DefaultFinalTime = defaultFinalTimeItem.DateTime;
		}

		void defaultDailyWorkingTimeItem_OnDateTimeChanged(object Sender, DateTime DateTime)
		{
			Persistance.Instance.DefaultDailyWorkingTime = DateTime.TimeOfDay;
		}

		public override string Name
		{
			get { return Resources.PanelOptionsName; }
		}

		public override void Bind(SenseListControl senseListControl)
		{
			base.Bind(senseListControl);

			defaultStartTimeItem.LabelText = string.Format("{0}:", Resources.DefaultStartTimeLabel);
			defaultFinalTimeItem.LabelText = string.Format("{0}:", Resources.DefaultFinalTimeLabel);
			defaultDailyWorkingTimeItem.LabelText = string.Format("{0}:", Resources.DefaultDailyWorkingTimeLabel);
			languageComboItem.LabelText = string.Format("{0}:", Resources.LanguageLabel);

			defaultStartTimeItem.DateTime = Persistance.Instance.DefaultStartTime;
			defaultFinalTimeItem.DateTime = Persistance.Instance.DefaultFinalTime;
			defaultDailyWorkingTimeItem.DateTime = DateTime.Now.Date + Persistance.Instance.DefaultDailyWorkingTime;

			languageComboItem.Items.Clear();
			object selectedLanguage = AvailableLanguage.en;
			foreach (KeyValuePair<object, string> language in EnumExtension.GetValues(typeof(AvailableLanguage)))
			{
				languageComboItem.Items.Add(new SensePanelComboItem.Item(language.Value, language.Key));
				string selectedLangugaeAsString = ((AvailableLanguage)language.Key).ToString();
				if (selectedLangugaeAsString == Persistance.Instance.SelectedLanguage.TwoLetterISOLanguageName) selectedLanguage = language.Key;
				if (selectedLangugaeAsString == Persistance.Instance.SelectedLanguage.ThreeLetterISOLanguageName) selectedLanguage = language.Key;
				if (selectedLangugaeAsString == Persistance.Instance.SelectedLanguage.Name) selectedLanguage = language.Key;
			}

			languageComboItem.OnSelectedValueChanged -= languageComboItem_OnSelectedValueChanged;
			languageComboItem.SelectedValue = selectedLanguage;
			languageComboItem.OnSelectedValueChanged += languageComboItem_OnSelectedValueChanged;

			senseListControl.AddItem(defaultStartTimeItem);
			senseListControl.AddItem(defaultFinalTimeItem);
			senseListControl.AddItem(defaultDailyWorkingTimeItem);
			senseListControl.AddItem(languageComboItem);
		}
	}
}
